package com.yundingai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yundingai.domain.entity.AllUser;
import com.yundingai.domain.entity.UserSocialData;
import com.yundingai.domain.vo.LoginVo;
import com.yundingai.exception.BusinessException;
import com.yundingai.mapper.AllRoleMapper;
import com.yundingai.mapper.UserSocialDataMapper;
import com.yundingai.service.AllUserService;
import com.yundingai.mapper.AllUserMapper;
import com.yundingai.vm.enums.ResultStatusEnum;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.prefs.BackingStoreException;

/**
 * @author liyuxiang
 * @description 针对表【all_user(用户信息表：记录用户信息)】的数据库操作Service实现
 * @createDate 2025-07-16 12:43:19
 */
@Service
public class AllUserServiceImpl extends ServiceImpl<AllUserMapper, AllUser>
        implements AllUserService {
    @Autowired
    private AllUserMapper allUserMapper;
    @Autowired
    private AllRoleMapper allRoleMapper;
    @Autowired
    private UserSocialDataMapper userSocialDataMapper;

    //保存头像
    @Override
    public void saveAvatarUrl(Integer userId, String avatarUrl) {
        allUserMapper.saveAvatarUrl(userId, avatarUrl);

    }

    //登录
    @Override
    public LoginVo login(String username, String password) {
        //参数校验
        if (username == null || username.isEmpty()) {
            throw new BusinessException(ResultStatusEnum.REQUIRE_USERNAME);
        }
        LoginVo loginVo = allUserMapper.login(username, DigestUtils.md5DigestAsHex(password.getBytes()));
        if (loginVo == null) {
            //如果用户名或密码错误，抛出异常
            throw new BusinessException(ResultStatusEnum.LOGIN_ERROR);
        }
        //校验是否被禁用
        if (loginVo.getStatus() == 0) {
            throw new BusinessException(ResultStatusEnum.USER_DISABLED);
        }
        return loginVo;
    }

    //注册
    @Override
    @Transactional
    public void register(String username, String password, String email) {
        //参数校验
        if (username == null || username.isEmpty()) {
            throw new BusinessException(ResultStatusEnum.REQUIRE_USERNAME);
        }
        AllUser allUser = new AllUser();
        allUser.setUsername(username);
        allUser.setEmail(email);
        //把密码加密
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(password.getBytes());
        allUser.setPassword(md5DigestAsHex);
        //检查用户名是否已存在
        List<AllUser> existingUser = allUserMapper.selectList(
                new LambdaQueryWrapper<AllUser>().eq(AllUser::getUsername, username)
        );
        if (!existingUser.isEmpty()) {
            throw new BusinessException(ResultStatusEnum.USERNAME_EXIST);
        }
        //检查邮箱是否已存在
        List<AllUser> existingEmail = allUserMapper.selectList(new LambdaQueryWrapper<AllUser>().eq(AllUser::getEmail, email));
        if (!existingEmail.isEmpty()) {
            throw new BusinessException(ResultStatusEnum.EMAIL_EXIST);
        }
        //注册用户
        allUserMapper.register(allUser);
        Integer userId = allUser.getUserId();
        //分配默认角色
        allRoleMapper.assignDefaultRole(userId);
        //创建用户社交信息表
        UserSocialData userSocialData = new UserSocialData();
        userSocialData.setUserId(userId);
        userSocialDataMapper.insert(userSocialData);
    }

    @Override
    public Integer findUserRole(String username) {
        Integer userRole = allUserMapper.findUserRole(username);
        return userRole;
    }

    //修改用户头像
    @Override
    public void updateAvatarUrl(Integer userId, String avatarUrl) {
        allUserMapper.updateAvatarUrl(userId, avatarUrl);


    }

    @Override
    public Integer getTotalUsers() {
        return allUserMapper.selectTotalUserCount();
    }

    @Override
    public void updateUserInfo(AllUser allUser) {
        allUserMapper.updateUserInfo(allUser);
    }

    @Override
    public void updatePassword(Integer userId, String password) {
        allUserMapper.updatePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes()));
    }

}




